package leetcode.simple.q189;

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] nums = {-1};
        rotate(nums, 3);

    }

    /**
     * 我自己写的解法
     * @param nums
     * @param k
     */
    public static void rotate(int[] nums, int k) {
        int[] arr = new int[nums.length];
        int nextIndex = 0;
        for (int i = 0; i < nums.length; i++) {
            nextIndex = i + k;
            //处理下标越界的问题
            if (nextIndex >= nums.length){
                nextIndex = nextIndex % nums.length;
            }
            arr[nextIndex] = nums[i];
        }
        System.arraycopy(arr, 0, nums, 0, nums.length);
        System.out.println(Arrays.toString(nums));
    }

    /**
     * 官方题解1
     * @param nums
     * @param k
     */
    public void rotate2(int[] nums, int k) {
        int temp, previous;
        for (int i = 0; i < k; i++) {
            previous = nums[nums.length - 1];
            for (int j = 0; j < nums.length; j++) {
                temp = nums[j];
                nums[j] = previous;
                previous = temp;
            }
        }
    }
}
